package com.example.lxc415billiards.controller;

import com.example.lxc415billiards.domain.Goods;
import com.example.lxc415billiards.service.GoodsService;
import com.example.lxc415billiards.untils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/goods")
@Api(tags = "商品管理-李效丞0415")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation("获取所有商品信息")
    @GetMapping("/getAllGoods")
    public AjaxResult getAllGoods() {
        List<Goods> goods = goodsService.getAllGoods();
        return AjaxResult.success(goods);
    }

    @ApiOperation("根据ID获取商品信息")
    @GetMapping("/getGoodsById/{id}")
    public AjaxResult getGoodsById(@PathVariable int id) {
        Goods goods = goodsService.getGoodsById(id);
        return goods != null ? AjaxResult.success(goods) : AjaxResult.error("商品不存在");
    }

    @ApiOperation("新增商品")
    @PostMapping("/addGoods")
    public AjaxResult addGoods(@RequestBody Goods goods) {
        Goods newGoods = goodsService.addGoods(goods);
        return AjaxResult.success(newGoods);
    }

    @ApiOperation("根据ID删除商品")
    @DeleteMapping("/deleteGoodsById/{id}")
    public AjaxResult deleteGoodsById(@PathVariable int id) {
        goodsService.deleteGoodsById(id);
        return AjaxResult.success("删除成功");
    }

    @ApiOperation("修改商品信息")
    @PutMapping("/updateGoods")
    public AjaxResult updateGoods(@RequestBody Goods goods) {
        Goods updatedGoods = goodsService.updateGoods(goods);
        return updatedGoods != null ? AjaxResult.success(updatedGoods) : AjaxResult.error("更新失败");
    }
}
